package com.mall.order.services;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mall.order.OrderQueryService;
import com.mall.order.converter.OrderConverterImpl;
import com.mall.order.dal.entitys.*;
import com.mall.order.dal.persistence.OrderItemMapper;
import com.mall.order.dal.persistence.OrderMapper;
import com.mall.order.dal.persistence.OrderShippingMapper;
import com.mall.order.dto.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;

/**
 *  ciggar
 * create-date: 2019/7/30-上午10:04
 */
@Slf4j
@Component
@Service
public class OrderQueryServiceImpl implements OrderQueryService {
    @Autowired
    OrderMapper orderMapper;
    @Autowired
    OrderItemMapper orderItemMapper;
    @Autowired
    OrderShippingMapper orderShippingMapper;

    @Autowired
    OrderConverterImpl orderConverter;


    @Override
    public OrderListResponse getOrderList(Long uid, Integer size, Integer page) {
        PageHelper.startPage(page, size);

        OrderListResponse orderListResponse = new OrderListResponse();

        Example example = new Example(Order.class);
        Example example1 = new Example(OrderItem.class);

        example.setOrderByClause("create_Time" + " " + "desc");

        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("userId", uid);

        List<Order> orders = orderMapper.selectByExample(example);
        List<OrderDetailInfo> orderDetailInfoList = new ArrayList<>();

        for (Order order : orders) {
            OrderDetailInfo orderDetailInfo = orderConverter.order2detail(order);
            example1.clear();
            Example.Criteria orderId = example1.createCriteria().andEqualTo("orderId", order.getOrderId());
            List<OrderItem> orderItems = orderItemMapper.selectByExample(example1);
            List<OrderItemDto> orderItemDtoList = new ArrayList<>();
            for (OrderItem orderItem : orderItems) {
                String[] split = orderItem.getPicPath().split(",");
                orderItem.setPicPath(split[0]);
                System.out.println("orderItem picture path = "+orderItem.getPicPath());
                OrderItemDto orderItemDto = orderConverter.item2dto(orderItem);
                orderItemDtoList.add(orderItemDto);
            }
            orderDetailInfo.setOrderItemDto(orderItemDtoList);
            orderDetailInfoList.add(orderDetailInfo);
        }

        // 分页
        PageInfo<Order> orderPageInfo = new PageInfo<>(orders);
        long total = orderPageInfo.getTotal();

        orderListResponse.setData(orderDetailInfoList);
        orderListResponse.setTotal(total);

        return orderListResponse;
    }

    @Override
    public OrderDetailResponseNew getOrderDetail(String userInfo, String orderId) {
        OrderDetailResponseNew orderDetailResponseNew = new OrderDetailResponseNew();
        orderDetailResponseNew.setUsername(userInfo);

        Order order = orderMapper.selectByPrimaryKey(orderId);
        orderDetailResponseNew.setOrderTotal(order.getPayment());
        orderDetailResponseNew.setUserId(order.getUserId());
        orderDetailResponseNew.setOrderStatus(order.getStatus());

        OrderShipping orderShipping = orderShippingMapper.selectByPrimaryKey(orderId);
        orderDetailResponseNew.setTel(orderShipping.getReceiverPhone());
        orderDetailResponseNew.setStreetName(orderShipping.getReceiverState()+orderShipping.getReceiverCity()+
        orderShipping.getReceiverDistrict()+orderShipping.getReceiverAddress());

        List<OrderItem> orderItems = orderItemMapper.queryByOrderId(orderId);
        List<OrderItemDto> orderItemDtoList = new ArrayList<>();
        for (OrderItem orderItem : orderItems) {
            String[] split = orderItem.getPicPath().split(",");
            orderItem.setPicPath(split[0]);
            OrderItemDto orderItemDto = orderConverter.item2dto(orderItem);
            orderItemDtoList.add(orderItemDto);
        }
        orderDetailResponseNew.setGoodsList(orderItemDtoList);
        return orderDetailResponseNew;
    }
}
